import BaseAd from "./BaseAd";
import { SDKConfig, ADName, ResultCallback, ResultState, NativeAdCallback, isNull, DataCallback, BannerDir, SDKState } from "../SDKConfig";
import { BaseShare } from "./BaseShare";
import BaseLogin from "./BaseLogin";
import BaseSubPackage from "./BaseSubPackage";
import BaseScrennshot from "./BaseScreenshot";
import BaseRecorder from "./BaseRecorder";
import FileSystemManager from "./FileSystemManager";
import TextureRenderUtils from "../../engine/TextureRenderUtils";
import BaseNativeAd from "./BaseNativeAd";
import RandomHelper from "../../cfw/tools/RandomHelper";
import BaseModel from "../../cfw/mvc/BaseModel";
import GameItemModel from "./GameItemModel";
import BaseCustomAd from "./BaseCustomAd";
import { showVideo } from "../../Adv_sdk";

export default class BaseChannel extends BaseModel {

    //渠道号
    protected channelID: number = 0

    protected insMap = {}

    //激励视频
    protected rewardAd: BaseAd = null;
    //banner广告实例
    protected bannerAd: BaseAd = null;
    //插屏广告
    protected insertAd: BaseAd = null;
    //分享实例
    protected share: BaseShare = null;
    //小游戏分包
    protected subPackage: BaseSubPackage = null;
    //原生广告
    protected nativeAd: BaseNativeAd = null;
    //盒子广告
    protected appBoxAd: BaseAd = null;
    //登陆
    protected loginMgr: BaseLogin = null;
    //录屏功能
    protected recorder: BaseRecorder = null;
    //截屏功能
    protected screenshot: BaseScrennshot = null;




    protected fileSystem: FileSystemManager = null;

    //渠道配置数据
    protected configData: any;
    //是否是回到主界面
    protected showFlag: boolean = false;

    protected customAd: BaseCustomAd = null;

    constructor(cID: number) {
        super()
        this.channelID = cID;
        this.configData = SDKConfig[this.channelID]
    }

    setInstance(key: string, ins) {
        this.insMap[key] = ins;
    }

    hasInstance(key: string) {
        return !isNull(this.insMap[key])
    }

    getInstance(key: string): BaseAd {
        return this.insMap[key]
    }

    hasParam(name: string): boolean {
        let param = this.configData[name]
        if (param == undefined || param == null) {
            return false;
        }
        if (Array.isArray(param)) {
            return param.length > 0
        }
        return true;
    }

    hasCustomAd() {
        return this.customAd != null;
    }

    showCustomAd(index: number, xr: number, yr: number) {
        if (this.hasCustomAd()) {
            this.customAd.setRx(xr)
            this.customAd.setRy(yr)
            this.customAd.open(this.getParam(index, ADName.customAd))
        }
    }

    hideCustomAd() {
        if (this.hasCustomAd()) {
            this.customAd.hide()
        }
    }


    getRandomParem(adName: string) {
        let list: string[] = this.configData[adName]
        if (list.length > 0) {
            let index = RandomHelper.random(0, list.length)
            return list[index]
        }
        return null;
    }

    getParamValue(adName: string) {
        return this.configData[adName]
    }

    getParam(site: number, adName: string) {
        let list: string[] = this.configData[adName]
        if (list.length > 0) {
            if (list.length > site) {
                return list[site]
            } else {
                return list[0]
            }
        } else {
            return null;
        }

    }

    /**
     * 是否有banner广告
     */
    hasBanner() {
        return this.hasParam(ADName.banner) && this.bannerAd != null;
    }
    /**
     * 显示banner广告
     * @param site 广告位索引
     */
    showBanner(site: number, dir: BannerDir = BannerDir.BOTTOM_LEFT) {
        if (this.hasBanner()) {
            let ad = this.getParam(site, ADName.banner)
            if (ad) {
                this.bannerAd.setDir(dir)
                this.bannerAd.updateSize()
                this.bannerAd.open(ad)
            }

        }
    }

    getBannerAd() {
        return this.bannerAd;
    }
    //隐藏banner广告
    hideBanner() {
        if (this.hasBanner()) {
            this.bannerAd.close()
        }
    }

    loadBanner(isShow?: boolean) {
        if (this.hasBanner()) {
            this.bannerAd.load(isShow)
        }
    }

    /**
     * 是否有插屏广告
     */
    hasInsertAd() {
        return this.hasParam(ADName.insert) && this.insertAd != null;
    }

    /**
     * 展示插屏广告
     */
    showInsertAd(site: number, func?: (r: number) => void) {
        if (this.hasInsertAd()) {
            let adId = this.getParam(site, ADName.insert)
            if (adId) {
                this.insertAd.open(adId, func)
            }

        }
    }

    closeInsertAd() {
        if (this.hasInsertAd()) {
            this.insertAd.close()
        }
    }

    //是否有激励视频广告
    hasRewardAd() {
        return this.hasParam(ADName.reward) && this.rewardAd != null;
    }
    //展示激励视频广告
    showRewardAd(site: number, callback: ResultCallback) {
        console.log("展示广告")
        // if (this.hasRewardAd()) {
        //     let adID = this.getParam(site, ADName.reward)
        //     if (adID) {
        //         this.rewardAd.open(adID, callback)
        //     }
        // } else {
        //     callback(ResultState.YES)
        // }

        showVideo(() => {
            callback(ResultState.YES)
        }, () => {

        })

    }

    //是否有分享能力
    hasShare() {
        return this.share != null;
    }

    /**
     * 分享
     * @param site 
     * @param callback 
     */
    showShare(site: number, callback: ResultCallback, isShowRecorder?: boolean) {
        if (this.hasShare()) {
            this.share.share(site, callback, isShowRecorder)
        }
    }



    //短震动
    vibrateShort() {

    }
    //展示网络图片
    previewImage(imgUrl: string) {

    }
    //跳转能力
    navigateToMiniProgram(appID: string) {

    }

    hasLogin() {
        return !isNull(this.loginMgr)
    }

    /**
     * 登陆游戏
     * @param account 
     * @param func 
     */
    login(account: string, func: ResultCallback) {
        if (this.hasLogin()) {
            this.loginMgr.login(account, func)
        } else {
            func(ResultState.YES)
        }
    }

    /**
     * 获取用户信息
     * @param withCredentials 
     * @param lang 
     * @param func 
     */
    getUserInfo(withCredentials: string, lang: string, func: DataCallback) {
        if (this.hasLogin()) {
            this.loginMgr.getUserInfo(withCredentials, lang, func)
        }
    }
    /**
     * 检查登陆状态
     * @param callback 
     */
    checkSession(callback: ResultCallback) {
        if (this.hasLogin()) {
            this.loginMgr.checkSession(callback)
        } else {
            callback(ResultState.YES)
        }
    }

    postMessage(msg: any) {

    }

    hasSubPackage() {
        return true
    }

    loadSubPackage(subNames: string[], callback: DataCallback) {
        if (this.hasSubPackage()) {
            this.subPackage.loadList(subNames, callback);
        } else {
            callback(ResultState.YES, null)
        }
    }

    hasScreenshot() {
        return this.screenshot != null;
    }

    initRender(camera: cc.Camera, renderNode: cc.Node) {
        if (this.hasScreenshot()) {
            this.screenshot.initRender(camera, renderNode)
        }
    }
    showScreenshot(renderHelper: TextureRenderUtils) {
        if (this.hasScreenshot()) {
            this.screenshot.capture(renderHelper)
        }
    }

    getNativeAd() {
        return this.nativeAd;
    }

    hasNativeAd() {
        return this.hasParam(ADName.native) && this.nativeAd != null;
    }

    createNativeAd(index: number) {
        if (this.hasNativeAd()) {
            let ad = this.getParam(index, ADName.native)
            if (ad) {
                this.nativeAd.create(ad)
            }
        }
    }

    isOpenNativeErrorClick() {
        return false
    }

    isBannerOpen() {
        return true
    }

    isInsertOpen() {
        return true
    }

    loadNativeAd() {
        if (this.hasNativeAd()) {
            this.nativeAd.load()
        }
    }

    openNativeView() {

    }

    showNativeAd(index: number, callback: NativeAdCallback) {
        if (this.hasNativeAd()) {
            let ad = this.getParam(index, ADName.native)
            if (ad) {
                this.nativeAd.open(ad, callback)
            }

        }
    }

    hideNativeAd() {
        if (this.hasNativeAd()) {
            this.nativeAd.close()
        }
    }
    reportAdClick(adId) {
        if (this.hasNativeAd()) {
            this.nativeAd.reportAdClick(adId)
        }
    }

    reportAdShow(adId) {
        if (this.hasNativeAd()) {
            this.nativeAd.reportAdShow(adId)
        }
    }

    hasRecorder() {
        return this.recorder != null;
    }

    recorderStart(obj?) {
        if (this.hasRecorder()) {
            this.recorder.start(obj)
        }
    }

    getRecorder() {
        return this.recorder;
    }


    recorderStop(isSave: boolean = true, callback: ResultCallback) {
        if (this.hasRecorder()) {
            this.recorder.stop(isSave, callback)
        }
    }


    hasAppBox() {
        return this.hasParam(ADName.appbox) && this.appBoxAd != null;
    }

    showAppBoxAd(index: number) {
        if (this.hasAppBox()) {
            let ad = this.getParam(index, ADName.appbox);
            if (ad) {
                this.appBoxAd.open(ad)
            }

        }
    }

    destroyAppBoxAd() {
        if (this.hasAppBox()) {
            this.appBoxAd.destroy()
        }
    }


    showToast(title: string) {

    }
    canInstallShortcut(func: ResultCallback) {
        func(ResultState.NO)
    }



    /**
     * 安装图标到桌面
     * @param result 
     */
    installShortcut(result: ResultCallback) {
        result(ResultState.NO)
    }


    cantUseImageDataURL() {
        return false;
    }

    hasFileSystem() {
        return this.fileSystem != null;
    }

    /**
     * 
     * @param filePath 
     * @param data 
     * @param encoding 
     * @param param 
     */
    writeFileSync(filePath: string, data: any, encoding: string, param?): boolean {
        if (this.hasFileSystem()) {
            return this.fileSystem.writeFileSync(filePath, data, encoding, param)
        }
        return false;
    }
    /**
     * 
     * @param filePath 
     * @param encoding 
     */
    readFileSync(filePath: string, encoding: string): any {
        if (this.hasFileSystem()) {
            return this.fileSystem.readFileSync(filePath, encoding)
        }
        return null;
    }

    readFile(filePath: string, encoding: string, callback: DataCallback) {
        if (this.hasFileSystem()) {
            this.fileSystem.readFile(filePath, encoding, callback)
        } else {
            callback(ResultState.NO, null)
        }
    }

    writeFile(filePath: string, data: any, encoding: string, callback: ResultCallback) {
        if (this.hasFileSystem()) {
            this.fileSystem.writeFile(filePath, data, encoding, callback)
        } else {
            callback(ResultState.NO)
        }

    }


    createCanvas() {
        let canvas = document.createElement('canvas')
        cc.log(' canvas ', canvas)
        return canvas;
    }



    //创建更多游戏按钮
    createMoreGameButton() {

    }

    hideMoreGameButton() {

    }
    //显示更多游戏面板
    showMoreGameView() {

    }
    //是否有更多游戏
    hasMoreGame() {
        return false;
    }

    // hasAds() {
    //     return true;
    // }

    hasSmallBanner() {
        return false;
    }




    onCheckForUpdate(callback: ResultCallback) {

        callback(ResultState.YES)

    }

    loginZS() {

    }

    loadZSConfig() {

    }

    //-------------------------------  所有广告整合到一个变量中管理。
    /**
     * 有广告也有实例
     * @param key 
     */
    hasAdAndInstance(key: string) {
        return this.hasAd(key) && this.hasInstance(key)
    }

    hasAd(name: string) {
        return this.hasParam(name)
    }

    showAd(name: string, index: number, call?: Function) {
        if (this.hasAd(name)) {
            let instance: BaseAd = this.getInstance(name)
            if (instance) {
                let ad = this.getParam(index, name)
                if (ad) {
                    instance.open(ad, call)
                }
            }
        }
    }

    closeAd(name: string) {
        if (this.hasAd(name)) {
            let instance: BaseAd = this.getInstance(name)
            if (instance) {
                instance.close()
            }
        }
    }

    destroyAd(name: string) {
        if (this.hasAd(name)) {
            let instance: BaseAd = this.getInstance(name)
            if (instance) {
                instance.destroy()
            }
        }
    }


    /**
     * 发送事件
     * @param eventName 事件名称
     * @param value value
     */
    sendEvent(eventName: string, value?: any) {

    }



    hasCrazyClick() {
        return false;
    }


    getAdData(): GameItemModel[] {
        return [];
    }

    loadAd() {

    }

    sortAdData() {

    }

    navigate2Mini(data: GameItemModel, success?: Function, fail?: Function, complete?: Function) {

    }

    toOtherGame(model: GameItemModel, success?: Function, fail?: Function, complete?: Function) {

    }
    setShowFlag(flag: boolean) {
        this.showFlag = flag;
    }

    getShowFlag() {
        return this.showFlag;
    }


    //开局看视频开关（1开 0关）
    isStartErrorClick() {
        return false;
    }

    isBannerErrorClick() {
        return false
    }

    hasBigExport() {
        return false;
    }

    isSwtichOpen() {
        return false;
    }

    battleEnd(id) {

    }
    battleStart(id) {

    }

    isOpenStartEgg() {
        return false;
    }

    getBannerDelayTime() {

        return 2;
        // return this.zsConfig.zs_banner_time;
    }

    getRandom_jump() {
        return 0;
    }

    getAuto_jump_switch() {
        return 0;
    }

    getAuto_jump_switch2() {
        return 0;
    }

    getzs_video_delayed() {

        return false;
    }

    getzs_banner_force_click() {
        return false;
    }
    getClick_award_video() {
        return 0;
    }

    getzs_click_award_num2() {
        return false;
    }

    getzs_loading_video() {
        return false;
    }

    getzs_advert_change() {
        return 0;
    }
}
